
import * as actions from './';
//   import BSPError from './../common/bsperror';
import HttpRequest from './../common/http';
  
const rootUrl = window.restConfig.rootUrl;

export const personalTasksInit = () => {
  return (dispatch, getState) => {
    dispatch({
    type: actions.PERSONAL_TASK_INIT
    })
  }
}
  
export const initTasks = (code) => {
  return (dispatch) => {

    HttpRequest.get(rootUrl + '/personal/task/'+code)
      .then((ret) => {
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE,
          data: ret.data
        })
      }).catch((err) => {
        dispatch(showError('加载数据失败'));
      });
  }
}

export const moreTasks = () => {
  return (dispatch,getStore) => {
    let state = getStore().personalTask || {};
    let oldData = state.data || [];
    HttpRequest.get(rootUrl + '/personal/more/task')
      .then((ret) => {
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE,
          data: {
            data: oldData.concat(ret.data.data)
          }
        })
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE,
          data: {
            page: ret.data.page
          }
        })
      }).catch((err) => {
        dispatch(showError('加载数据失败'));
      });
  }
}

export const confirmTask = (enlistId, index) => {
  return (dispatch,getStore) => {
    let state = getStore().personalTask || {};
    let oldData = state.data || [];
    let newData = oldData.splice(index, 1);
    HttpRequest.post(rootUrl + '/activity/customer/confirm',{enlistId:enlistId})
      .then((ret) => {
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE
        })
        dispatch(confirmChange('confirmFailMsg', '确认学习计划成功'));
        dispatch(confirmChange('showConfirmSuccess', true));
        // dispatch(initTasks(code));   这种今天不行了,code不能重复使用???   等pc预约通了再试试下面
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE,
          data: {
            data: newData
          }
        })
        let successTimer = setTimeout( () => {
          window.clearTimeout(successTimer);
          dispatch(confirmChange('showConfirmSuccess', false))
        }, 2000);
      }).catch((err) => {
        dispatch(confirmChange('confirmFailMsg', err.message));
        dispatch(confirmChange('showConfirmfail', true));
        let failTimer = setTimeout( () => {
          window.clearTimeout(failTimer);
          dispatch(confirmChange('showConfirmfail', false))
        }, 2000);
        dispatch(showError('确认学习计划失败'));
      });
    }
  }
  
  export const cancelTask = (enlistId, index) => {
    return (dispatch,getStore) => {
      let state = getStore().personalTask || {};
      let oldData = state.data || [];
      let newData = oldData.splice(index, 1);
      HttpRequest.post(rootUrl + '/activity/customer/cancel',{enlistId:enlistId})
      .then((ret) => {
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE
        })
        dispatch(confirmChange('confirmFailMsg', '取消学习计划成功'));
        dispatch(confirmChange('showConfirmSuccess', true))
        // dispatch(initTasks(code));
        dispatch({
          type: actions.PERSONAL_TASK_CHANGE,
          data: {
            data: newData
          }
        })
        let successTimer = setTimeout( () => {
          window.clearTimeout(successTimer);
          dispatch(confirmChange('showConfirmSuccess', false))
        }, 2000);
      }).catch((err) => {
        dispatch(confirmChange('confirmFailMsg', err.message));
        dispatch(confirmChange('showConfirmfail', true));
        let failTimer = setTimeout( () => {
          window.clearTimeout(failTimer);
          dispatch(confirmChange('showConfirmfail', false))
        }, 2000);
        dispatch(showError('取消学习计划失败'));
      });
  }
}

export const dataChange = (name, value) => {
  return {
    type: actions.PERSONAL_TASK_CHANGE,
    data: {
      data: {
        [name]: value
      }
    }
  }
}

export const confirmChange = (name, value) => {
  return {
    type: actions.PERSONAL_TASK_CHANGE,
    data: {
      state:{
        [name]: value
      }
    }
  }
}
export const errorChange = (name, value) => {
  return {
    type: actions.PERSONAL_TASK_CHANGE,
    data: {
      error: {
        [name]: value
      }
    }
  }
}

export const showError = (text) => {
  return (dispatch) => {
    dispatch(errorChange('hasError', true));
    dispatch(errorChange('msg', text));

    let timer = setTimeout( () => {
      window.clearTimeout(timer);
      dispatch(errorChange('hasError', false));
      dispatch(errorChange('msg', ''));
    }, 2000);
  }
}